package com.chen.test;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.chen.sql.Question;
import com.chen.sql.QuestionList;
import com.google.gson.Gson;

import gen.R;
import gui.Activity;
import gui.framework.intent.Bundle;
import gui.reader.TextFileUtils;
import gui.sql.DBHelper;
import gui.view.Button;
import gui.view.View;
import gui.view.View.OnClickListener;

public class ExportActivity extends Activity {
	private Button ex_all, ex_re;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.export_activity);
		findView();
		addListener();
	}

	private void addListener() {
		ex_all.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				QuestionList questionList = new QuestionList();
				List<Question> list = DBHelper.queryEntityList(Question.class, "select * from question");
				questionList.setList(list);
				try {
					TextFileUtils file = new TextFileUtils("e://[e]xport",
							"export" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".txt");
					String json = new Gson().toJson(questionList);
					file.append(json);
					file.close();
				} catch (IOException e) {
				}
				DBHelper.update("update question set export=? ", 1);
				onFinish();
			}
		});
		ex_re.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				QuestionList questionList = new QuestionList();
				List<Question> list = DBHelper.queryEntityList(Question.class, "select * from question where export=?",
						0);
				questionList.setList(list);
				try {
					TextFileUtils file = new TextFileUtils("e://[e]xport",
							"export" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".txt");
					String json = new Gson().toJson(questionList);
					file.append(json);
					file.close();
				} catch (IOException e) {
				}
				DBHelper.update("update question set export=? ", 1);
				onFinish();
			}
		});
	}

	private void findView() {
		ex_all = (Button) findViewById(R.id.export_all);
		ex_re = (Button) findViewById(R.id.export_shengxiade);
	}

	@Override
	protected void onDestory() {
		startActivity(MainActivity.class);
	}
}
